package image.synth;

import image.ImageGroup;
import image.ps.PixelShader;
import math.rand.RNG;
import math.vec2i;

public class TileSynth implements Runnable
{
    
    public ImageGroup rTarget;
    
    public PixelShader rPS;
    
    private vec2i mStart;
    
    private vec2i mSize;
    
    private vec2i mImageSize;
    
    private RNG mRNG;
    
    
    public TileSynth(
            vec2i aStart, 
            vec2i aSize, 
            vec2i aImageSize, 
            long aSeed)
    {
        this.rTarget = null;
        this.rPS = null;
        this.mStart = aStart.copy();
        this.mSize = aSize.copy();
        this.mImageSize = aImageSize.copy();
        this.mRNG = new RNG(aSeed);
    }
    
    
    public void run()
    {
        // Let the PixelShader use this tile's RNG:
        this.rPS.rRNG = this.mRNG;
        
        // Full tile pass in sequence:
        vec2i imagePixel = new vec2i();
        for (int j = 0; j < this.mSize.y; j++)
        {
            imagePixel.y = j + this.mStart.y;
            for (int i = 0; i < this.mSize.x; i++)
            {
                imagePixel.x = i + this.mStart.x;
                
                // Shade the pixel at the current location: 
                this.rPS.shade(
                    this.rTarget, 
                    imagePixel, 
                    this.mImageSize
                );
            }
        }
        
        // Take back the tile's RNG from the PixelShader.
        this.rPS.rRNG = null;
    }
    
}
